package com.iiapk.location;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CallerLocationAdapter {

	private final static String TAG = "CallerLocationAdapter";
	private DataBaseHelper mDBHelper = null;

	public CallerLocationAdapter(Context context) {
		int databaseVersion = 1;
		mDBHelper = new DataBaseHelper(context, databaseVersion);
	}

	public SQLiteDatabase open() throws SQLException {
		return mDBHelper.getReadableDatabase();
	}

	public void close() {
		mDBHelper.close();
	}

	private class DataBaseHelper extends SQLiteOpenHelper {

		private final Context mContext;
		private static final String mName = "CallerLoc.db";
		private SQLiteDatabase mDatabase = null;
		private boolean mIsInitializing = false;
		private final int mNewVersion;

		DataBaseHelper(Context context, int database_version) {
			super(context, mName, null, database_version);
			mNewVersion = database_version;
			this.mContext = context;
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
		}

		private void databaseInit() {
			AssetManager am = mContext.getAssets();
			if (null == am) {
				Log.e(TAG, "databaseInit, get assets failed!");
				return;
			}
			OutputStream out = null;
			try {
				//String[] filenames = am.list("databases");
				out = mContext.openFileOutput("CallerLoc.db",Context.MODE_PRIVATE);
				InputStream in = am.open("databases/CallerLoc.db");
				byte[] buffer = new byte[1024 * 1024 * 6];
				int count = in.read(buffer);
				out.write(buffer, 0, count);
				out.flush();
				in.close();
			} catch (IOException e) {
				Log.e(TAG, e.getMessage(), e);
			} finally {
				if (out != null) {
					try {
						out.close();
					} catch (IOException e) {
						Log.e(TAG, e.getMessage(), e);
					}
				}
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		}

		public synchronized SQLiteDatabase getWritableDatabase() {
			throw new RuntimeException(TAG
					+ "method getWritableDatabase() should never be used");
		}

		public synchronized SQLiteDatabase getReadableDatabase() {
			if (mDatabase != null && mDatabase.isOpen()) {
				return mDatabase; 
			}

			if (mIsInitializing) {
				throw new IllegalStateException("getReadableDatabase called recursively");
			}
			SQLiteDatabase db = null;
			try {
				mIsInitializing = true;
				File file = mContext.getFileStreamPath(mName);
				if (!file.exists())
					databaseInit();
				String path = file.getPath();
				db = SQLiteDatabase.openDatabase(path, null,
						SQLiteDatabase.OPEN_READONLY);
				if (db.getVersion() != mNewVersion) {
					throw new SQLiteException(
							"Can't upgrade read-only database from version "
							+ db.getVersion() + " to " + mNewVersion+ ": " + path);
				}
				onOpen(db);
				Log.w(TAG, "Opened " + mName + " in read-only mode");
				mDatabase = db;
				return mDatabase;
			}
			finally {
				mIsInitializing = false;
				if (db != null && db != mDatabase)
					db.close();
			}
		}

		public synchronized void close() {
			if (mIsInitializing)
				throw new IllegalStateException("Closed during initialization");

			if (mDatabase != null && mDatabase.isOpen()) {
				mDatabase.close();
				mDatabase = null;
			}
		}
	}
}
